/**
 * 
 */
package org.rhq.plugins.tools.httpcheck;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import org.apache.log4j.Logger;
import org.rhq.core.domain.configuration.Configuration;

/**
 * @author lgao
 *
 */
class MailSender extends Thread implements HttpCheckerConstants{
	
	private static final Logger LOG = Logger.getLogger(MailSender.class);
	
	private Properties sendProps;
	
	private String mailFrom;
	
	private boolean useAuth;
	
	private String userName;
	
	private String password;
	
	private Configuration mailDetailConfig;
	
	private String path;
	
	protected MailSender(Configuration mailResConfig, Configuration _mailDetailConfig, String _path) throws Exception{
		super();
		this.path = _path;
		this.mailDetailConfig = _mailDetailConfig;
		sendProps = new Properties();
		String smtpHost = mailResConfig.getSimpleValue(RES_CONFIG_SENDER_HOST, null);
		mailFrom = mailResConfig.getSimpleValue(RES_CONFIG_SENDER_MAIL_FROM, null);
		
		useAuth = Boolean.valueOf(mailResConfig.getSimpleValue(RES_CONFIG_SENDER_AUTH, "false"));
		userName = mailResConfig.getSimpleValue(RES_CONFIG_SENDER_USER, null);
		password = mailResConfig.getSimpleValue(RES_CONFIG_SENDER_PWD, null);
		if(null == smtpHost || mailFrom == null){
			throw new IllegalStateException("SMTP Host should be configured.");
		}
		if(useAuth && (userName == null || password == null)){
			throw new IllegalStateException("Auth of the SMTP should be configured.");
		}
		
		// smtp port
		String smtpPort = mailResConfig.getSimpleValue(RES_CONFIG_SMTP_PORT, "25");
		
		sendProps.put("mail.smtp.host", smtpHost);
		sendProps.put("mail.smtp.auth", useAuth);
		sendProps.put("mail.transport.protocol", "smtp");
		sendProps.put("mail.smtp.port", smtpPort);
		
	}
	
	
	@Override
	public void run() {
		Session session = null;
		if(useAuth){
			LOG.info("Use Auth for SMTP.");
			session = Session.getInstance(sendProps, new SMTPAutenticator());
		}else{
			session = Session.getInstance(sendProps);
		}
		Message msg = new MimeMessage(session);
		try {
			msg.setFrom(new InternetAddress(mailFrom));
			msg.setRecipient(RecipientType.TO, new InternetAddress(this.mailDetailConfig.getSimpleValue(RES_CONFIG_ADMIN_EMAIL, "")));
			msg.setSubject(this.mailDetailConfig.getSimpleValue(RES_CONFIG_SUBJECT, ""));
			String text = this.mailDetailConfig.getSimpleValue(RES_CONFIG_CONTENT, "");
			if(path != null){
				text = text + "\r\n Page is: " + path;
			}
			msg.setText(text);
			Transport.send(msg);
			LOG.info("End sendming Mail.");
		} catch (Exception e) {
			throw new IllegalStateException("Can not send mail", e);
		}
		
	}
	
	
	private class SMTPAutenticator extends Authenticator{
		
		@Override
		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(userName, password);
		}
	}
	

}
